<?php  
class CategoryController extends AuthController{
	private $db;
	private $cid;
	private $category;
	public function __init(){

		$this->access();//权限验证

		$this->db=K('Category');//扩展模型CategoryModel

		$this->cid=Q('cid',0,'intval');

		$this->category=S('category');

		if ($this->cid && !isset($this->category[$this->cid])) {
			
			$this->error('栏目不存在');
		}
	}
	// 显示列表
	public function index(){
		$data=$this->category;
		
		if (!empty($this->cid)) {
			
			$data=Data::channelList($data,$this->cid); 
			
		}

		int_to_string($data,array('is_show'=>array(0=>"<a href='javascript:;' class='no' onclick='_show(this);'></a>",1=>"<a href='javascript:;' class='yes' onclick='_show(this);'></a>")));

		$this->assign('category',$data);

		$this->assign('cat',$this->category);
		
		$this->display();
	}
	// 添加栏目
	public function add(){
		if (IS_POST) {
			if ($this->db->addCategory()) {

				$this->success('添加成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{

			$cid=Q('cid',0,'intval');

			$this->assign('cid',$cid);
			
			$this->assign('category',S('category'));

			$this->display();
		}
	}
	// 编辑栏目
	public function edit(){
		if (IS_POST) {
			if ($this->db->editCategory()) {

				$this->success('编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{

			$data=$this->db->where('cid='.$this->cid)->find();
			
			$this->assign('data',$data);

			$this->assign('category',S('category'));

			$this->display();
		}
	}
	// 删除栏目
	public function del(){
		if ($this->db->delCategory()) {

			$this->success('删除成功','index');
		}else{
			$this->error('删除失败');
		}
	}
	//获取某一栏目的下级子栏目
	public function getSon(){

		$data=$this->db->getCategory();

		int_to_string($data,array('is_show'=>array(0=>"<a href='javascript:;' class='no' onclick='_show(this);'></a>",1=>"<a href='javascript:;' class='yes' onclick='_show(this);'></a>")));
		
		echo json_encode($data);exit;
	}
	// 更新缓存
	public function update(){
		if ($this->db->updateCache()) {
			$this->success('更新缓存成功','index');
		}else{
			$this->error('更新缓存失败');
		}
	}
	//异步更改栏目是否显示
	public function _show(){
		if ($this->db->updateShow()) {
			echo json_encode(array('state' => 1, 'msg' => ' 修改成功 ','timeout'=>0.3));exit;
		}else{
			echo json_encode(array('state' => 0, 'msg' => ' 修改失败 ','timeout'=>0.3));exit;
		}
	}
}
?>